package com.jt.blog.service;

import com.jt.blog.dao.TagMapper;
import com.jt.blog.domain.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tang
 * @data 2021/10/13
 * @apiNode
 */
@Service
public class TagServiceImpl implements TagService{

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Tag> selectTags() {
        //1.从redis查询tag信息，redis有则直接返回
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<Tag> tags = (List<Tag>) valueOperations.get("tags");
        if (tags != null || !tags.isEmpty()) return tags;

        //2.从redis没有查询到tag信息，查询mysql
        tags = tagMapper.selectList(null);

        //3.将从mysql查询到tag信息存储到redis
        valueOperations.set("tags", tags);

        //4.返回查询结果
        return tags;
    }
}
